package com.ChenYaRou.www.controller.servlet;

import com.ChenYaRou.www.controller.MessageServletController;
import com.ChenYaRou.www.controller.NoticeServletController;
import com.ChenYaRou.www.po.*;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import static com.ChenYaRou.www.constant.Constant.SHOW;
import static com.ChenYaRou.www.constant.NoticeConstant.*;

/**
 * @ClassName GetNoticeServlet
 * @Description TODO
 * @Author 86183
 * @Date 2022/5/3 21:03
 * @Version 1.0
 **/
public class GetNoticeServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PrintWriter out = resp.getWriter();
        JSONObject object = JSON.parseObject(req.getParameter("json"));
        Object mode = object.get("mode");
        NoticeServletController controller = new NoticeServletController();
        if(GET_NOTICE.equals(mode)){
            out.write(controller.getNotice(
                    new Role(Integer.parseInt(String.valueOf(object.get("id"))), (String) object.get("role"))));
        }else if(COMPLAINT.equals(mode)){
            Notice noticeInsert=new Notice();
            noticeInsert.setState(object.getBoolean("state"));
            noticeInsert.setContent(object.getString("content"));
            noticeInsert.setUpdateTime(LocalDateTime.now());
            if(BAN.equals(object.get(TYPE))){
                out.write(new NoticeServletController().complaint(noticeInsert,object.getString("name")));
            }else{
                noticeInsert.setRecipientId(object.getInteger("recipientId"));
                out.write(controller.addNotice(noticeInsert));
            }
        }else if(BAN.equals(mode)){
            Notice notice=new Notice(object.getString("content"), LocalDateTime.now(),
                    false, object.getInteger("recipientId"));
            if(USER.equals(object.getString(TYPE))){
                User userOld=new User();
                userOld.setId(object.getInteger("recipientId"));

                User userNew=new User();
                userNew.setState(object.getString("state"));
                out.write(new NoticeServletController().closeOrActive(notice,userNew,userOld));
            }else{
                Shop shopOld=new Shop();
                shopOld.setId(object.getInteger("shopId"));

                Shop shopNew=new Shop();
                shopNew.setState(object.getString("state"));

                out.write(new NoticeServletController().closeOrActive(notice,shopNew,shopOld));
            }

        } else if(RE_CONTENT.equals(mode)){
            out.write(new NoticeServletController().reContent(object.getString("content"),
                    (Boolean) object.get("state"),object.getInteger("recipientId"),
                    object.getInteger("parentId"),object.getInteger("shopId"),
                    object.getString("reContent"),object.getInteger("userId")));
        }else if(LIKES.equals(mode)){
            out.write(new NoticeServletController().like(object.getString("content"),
                    (Boolean) object.get("state"),object.getInteger("recipientId"),
                    object.getInteger("parentId")));
        }else if(CONTENT.equals(mode)){
            out.write(new MessageServletController().addMessage(new Message(object.getInteger("shopId"),
                    object.getString("content"),object.getInteger("senderId"),
                    LocalDateTime.now(),0,0,SHOW)));
        }else if(UPDATE_NOTICE.equals(mode)){
            List<Notice> newNotice=new ArrayList<>();
            List<Notice> oldNotice=new ArrayList<>();
            if(SEND_NOTICE.equals(object.get(TYPE))){
                Notice notice=new Notice();
                notice.setId(object.getInteger("noticeId"));
                newNotice.add(
                        new Notice(object.getString(CONTENT),LocalDateTime.now(),
                                false, object.getInteger("recipientId")));
                oldNotice.add(notice);
                out.write(new NoticeServletController().updateNotice(
                        newNotice,oldNotice,null,null));
            }else if(PASS_CREAT_NEW_SHOP.equals(object.get(TYPE))){
                out.write(new NoticeServletController().passCreatNewShop(
                        object.getString("content"), object.getInteger("noticeId"),
                        object.getInteger("recipientId")));
            }
        }
        out.flush();
        out.close();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
